﻿using System.Collections.Generic;
using System.Linq;
using CID.Domain.Development;
using CID.Domain.Infrastructure;
using NailsFramework.Persistence;

namespace CID.Domain.Audit
{
    public class VersionDeploymentLog : Model<VersionDeploymentLog>, IVersionDeploymentLog
    {
        public virtual int Id { get; private set; }
        public virtual IReleaseDeploymentLog ReleaseDeploymentLog { get; set; }
        public virtual IVersion Version { get; private set; }
        public virtual IActivationTemplate ActivationTemplate { get; private set; }
        public virtual IDeactivationTemplate DeactivationTemplate { get; private set; }
        public virtual IDeploymentTemplate DeploymentTemplate { get; private set; }

        private IList<IParameterDeploymentLog> parameters = new List<IParameterDeploymentLog>();
        public virtual IEnumerable<IParameterDeploymentLog> Parameters { get { return parameters; } }

        protected VersionDeploymentLog()
        { 
        }

        internal VersionDeploymentLog(IReleaseDeploymentLog deploymentLog, IVersion version) : this()
        {
            ReleaseDeploymentLog = deploymentLog;
            Version = version;
            ActivationTemplate = Version.Component.ActivationDeactivationDefinition.ActivationTemplate;
            DeactivationTemplate = Version.Component.ActivationDeactivationDefinition.DeactivationTemplate;
            DeploymentTemplate = Version.DeploymentDefinition.DeploymentTemplate;

            foreach (IParameterValue parameterValue in Version.Component.ActivationDeactivationDefinition.GetActivationParamaterValues(ReleaseDeploymentLog.Environment))
                parameters.Add(new ActivationParameterDeploymentLog(this, parameterValue));

            foreach (IParameterValue parameterValue in Version.Component.ActivationDeactivationDefinition.GetDeactivationParamaterValues(ReleaseDeploymentLog.Environment))
                parameters.Add(new DeactivationParameterDeploymentLog(this, parameterValue));

            foreach (IParameterValue parameterValue in Version.DeploymentDefinition.GetDeploymentParamaterValues(ReleaseDeploymentLog.Environment))
                parameters.Add(new DeploymentParameterDeploymentLog(this, parameterValue));
        }

        public virtual IEnumerable<IDeploymentParameterDeploymentLog> DeploymentParameters
        {
            get { return parameters.OfType<IDeploymentParameterDeploymentLog>().ToArray(); }
        }

        public virtual IEnumerable<IActivationParameterDeploymentLog> ActivationParameters
        {
            get { return parameters.OfType<IActivationParameterDeploymentLog>().ToArray(); }
        }

        public virtual IEnumerable<IDeactivationParameterDeploymentLog> DeactivationParameters
        {
            get { return parameters.OfType<IDeactivationParameterDeploymentLog>().ToArray(); }
        }
    }
}
